#ifndef IMSI_CORE_H
#define IMSI_CORE_H

#include "Defines.h"
#include "Lua-Main.h"

class Ui;

class Core : boost::noncopyable
{
private:
    std::vector<String> mSections;
    Ui* mGui;
    ScriptEngine mEngine;
    bool CheckForSection(const String& aCall, const String& aSection);
    void ShowSectionEvent(const String& aSection);

    template <class IT_>
    void MoveToShowableSection(IT_ aStart, IT_ aEnd)
    {
        if (aStart == aEnd) {
            throw out_of_range("Current position is not recognized");
        }

        while (++aStart != aEnd) {
            if (!ShouldSkipSection(*aStart)) {
                ShowSectionEvent(*aStart);
                break;
            }
        }
    }

    void MoveToNextShowableSection();
    void MoveToPreviousShowableSection();

public:
    bool NextCommand();
    bool BackCommand();
    bool ShouldSkipSection(const String& aSection);
    void Initialize(const std::list<String>& aFiles);
    void GuiDestroyed() { mGui = 0; }
    void InitializeSection(const String& aSection);
    String CurrentSection();

private:
    // This section contains routines which are called exclusively from scripting code
    friend class ScriptEngine;
    void StartUi();
    void Log(const String& aMessage);
    void SetLabelText(const String& aSection, const String& aLabel, const String& aText);

public:
    void Connect (Ui& aGui);
    int RunScript();
};

#endif // IMSI_CORE_H
